﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace QLTV_GiaoDien
{
    public partial class uc_DonVi : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_DonVi()
        {
            InitializeComponent();
        }
        public void GetLoad(int kt)
        {
            txt_Load.Text = kt.ToString();
        }
        DonVi_BLL donvi_bll = new DonVi_BLL();
        private void uc_DonVi_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = donvi_bll.LayDuLieu();
            txt_Load.Hide();
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_DonVi f = new frm_DonVi();
            f.Flag=0;
            f.MyGetData = new frm_DonVi.GetInt(GetLoad);

            f.ShowDialog();
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_DonVi f = new frm_DonVi();
            f.Flag = 1;
            f.MyGetData = new frm_DonVi.GetInt(GetLoad);
            f.MaDonVi =Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Đơn Vị").ToString());
            f.TenDonVi =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đơn Vị").ToString();
            f.GhiChu = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            f.ShowDialog();
        }

        private void bbi_Xem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_DonVi f = new frm_DonVi();
            f.Flag = 2;
            f.MyGetData = new frm_DonVi.GetInt(GetLoad);
            f.MaDonVi = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Đơn Vị").ToString());
            f.TenDonVi = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đơn Vị").ToString();
            f.GhiChu = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            f.ShowDialog();
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            frm_DonVi f = new frm_DonVi();
            f.Flag = 2;
            f.MyGetData = new frm_DonVi.GetInt(GetLoad);
            f.MaDonVi = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Đơn Vị").ToString());
            f.TenDonVi = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đơn Vị").ToString();
            f.GhiChu = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            f.ShowDialog();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void bbi_Xoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DonVi_Public dv = new DonVi_Public();
            dv.MaDonVi = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Đơn Vị").ToString());
            dv.TenDonVi = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đơn Vị").ToString();
            if(XtraMessageBox.Show("Bạn có muốn xóa Tên đơn vị: '"+dv.TenDonVi+"' có mã Đơn vị là: '"+dv.MaDonVi+"' này không?","Thông Báo",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
            {
                //
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Xóa";
                nk.MoTa = "Xóa thông tin Đơn vị: '" + dv.TenDonVi + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                //
                donvi_bll.Delete_DonVi(dv);
                uc_DonVi_Load(sender,e);
            }
        }

        private void bbi_Refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            uc_DonVi_Load(sender,e);
        }

        private void txt_Load_TextChanged(object sender, EventArgs e)
        {
            if (txt_Load.Text == "1")
            {
                uc_DonVi_Load(sender, e);
                txt_Load.Text = "0";
            }
        }

    }
}
